<?php

class BShopSkuValue extends BaseModel
{
    public $res;
    public $expire;

    public function getSource()
    {
        return "b_shop_sku_value";
    }

    public function keyValueList($kid)
    {
        $phql = "SELECT 
                        a.id as value_id,
                        a.value_name,
                        b.key_name,
                        b.id as key_id
                FROM BShopSkuValue AS a LEFT JOIN BShopSkuKey AS b ON a.key_id = b.id WHERE b.id IN ($kid)";
        $rows = $this->modelsManager->executeQuery($phql);
        $result = array();
        if ($rows) {
            $temp = $rows->toarray();
            foreach ($temp as $val) {
                $tmp[$val['key_name'].'-'.$val['key_id']][] = $val;
            }
            $result = [];
            foreach ($tmp as $key => $val) {
                $keyIds = explode('-',$key);
                $kTemp = [];
                $kTemp['key_name']   = $keyIds[0];
                $kTemp['key_id']   = $keyIds[1];
                foreach ($val as $vVal){
                    $valTemp['value_id'] = $vVal['value_id'];
                    $valTemp['value_name'] = $vVal['value_name'];
                    $kTemp['value'][] = $valTemp;
                }

                $result[]       = $kTemp;
            }
        }
        return $result;
    }


    public function getValueName($value_ids)
    {
        $id                  = implode(',', explode('_', $value_ids));
        $where['conditions'] = "id in ($id)";
        $valueArr            = $this->find($where)->toarray();
        $sku_name            = '';
        if (!empty($valueArr)) {
            $sku_name = implode(' ', array_column($valueArr, 'value_name'));
        }
        return $sku_name;
    }
}
